{{ $siteMap := dict }}
{{/* 获取当前时间戳 */}}
{{ $currentTime := now.Unix }}
{{ $secondsPerDay := 86400 }}
{{ $days := 8 }}
{{/* 时间跨度（秒） */}}
{{ $fiveDayAgo := sub $currentTime (mul $days $secondsPerDay) }}

{{/*{{ range $key, $value := .Site.Data }}*/}}
{{/*    {{ partial "helpers/log" (dict "message" (printf "可用数据文件: %v" $key )) }}*/}}
{{/*{{ end }}*/}}

{{/* 加载历史数据 */}}
{{ $historyData := .Site.Data.historySiteList | default dict}}
{{ if $historyData }}
    {{ $debugContent := $historyData | jsonify | truncate 100 }}
    {{ partial "helpers/log" (dict "message" (printf "成功加载站点历史数据（前100字符）: %v" $debugContent)) }}
{{ else }}
    {{ partial "helpers/log" (dict "level" "WARN" "message" "站点历史数据为空或未找到") }}
{{ end }}

{{/*{{ partial "helpers/log" (dict "message" (printf "【当前时间】%v" $currentTime )) }}*/}}

{{/* 第一步：处理当前 webstack 数据 */}}
{{ range .Site.Data.webstack }}
    {{ $taxonomy := .taxonomy }}
    {{ range .list }}
        {{ $term := (printf "%v/%v" $taxonomy .term) }}
{{/*        {{ partial "helpers/log" (dict "message" (printf "【站点分类】%v" $term )) }}*/}}
        {{ range .links }}
            {{ $url := .url }}
{{/*            {{ partial "helpers/log" (dict "message" (printf "[站点] 开始处理 %v" .url)) }}*/}}
            {{/* 检查历史数据中是否存在 */}}
            {{ $historyEntry := index $historyData.sites $url }}
            {{/* 默认状态为新资源 */}}
            {{ $status := "new" }}
            {{/* 创建时间 */}}
            {{ $createTime := $currentTime }}
            {{/* 更新时间 */}}
            {{ $updateTime := $currentTime }}

            {{/* 存在于历史数据中 */}}
            {{ if $historyEntry }}
{{/*                {{ partial "helpers/log" (dict "message" (printf "匹配到历史数据 %v - %d (%T) " ($historyEntry | jsonify) (int $historyEntry.create_time) $historyEntry.create_time)) }}*/}}
                {{ $historyDataCreateTime := (printf "%v (%v)" (int $historyEntry.create_time) (dateFormat "2006-01-02" (int $historyEntry.create_time))) }}
                {{ $CurrnetCreateTime := (printf "%v (%v)" $createTime (dateFormat "2006-01-02" $createTime )) }}
{{/*                {{ partial "helpers/log" (dict "message" (printf "当前创建时间 %v ~ 历史创建时间 %v" $CurrnetCreateTime $historyDataCreateTime)) }}*/}}

                {{/* 计算历史创建时间距今天数 */}}
                {{ $daysSinceCreation := div (sub $currentTime $historyEntry.update_time) 86400 }}
                {{ if gt $daysSinceCreation $days }}
                    {{/* 创建时间早于n天前 - 稳定资源 */}}
                    {{ $status = "stable" }}
                {{ else }}
                    {{/* 创建时间在n天内 - 新资源 */}}
                    {{ $status = "new" }}
                {{ end }}

                {{/* 获取创建时间 */}}
                {{ $createTime = $historyEntry.create_time }}
                {{/* 获取更新时间 */}}
                {{ $updateTime = $historyEntry.update_time }}
            {{/* 不存在于历史数据中 */}}
            {{ else }}
                {{ $status = "new" }}
            {{ end }}

            {{/* 添加到结果映射 */}}
            {{ if not (isset $siteMap .url) }}
                {{ if eq $status "new"}}
                    {{ partial "helpers/log" (dict "message" (printf "[站点] %v （%v）" .url $status)) }}
                {{ end }}
                {{ $siteMap = merge $siteMap (dict .url (dict
                    "title" (.title | default "")
                    "category" ($term | default "未知分类")
                    "status" $status
                    "description" (.description | default "")
                    "create_time" ($createTime | default "")
                    "update_time" ($updateTime | default "")
                    "remark" (printf "创建时间：%v | 更新时间：%v" (dateFormat "2006-01-02 15:04:05" (int $createTime)) (dateFormat "2006-01-02 15:04:05"  (int $updateTime)))
                    ))
                }}
            {{ end }}
        {{ end }}
    {{ end}}
{{ end }}

{{/* 第二步：处理废弃资源（在历史数据中但不在当前 webstack 中） */}}
{{ $deprecatedSites := dict }}
{{ range $url, $entry := $historyData.sites }}
    {{ if not (isset $siteMap $url) }}
        {{/* 计算距今天数 */}}
        {{ $daysSinceCreation := div (sub $currentTime (int $entry.create_time)) 86400 }}
        {{ $status := cond (gt $daysSinceCreation $days) "deprecated" "new" }}

        {{ partial "helpers/log" (dict "message" (printf "[站点] %v （%v）创建于%d天前" $url $status (int $daysSinceCreation))) }}

        {{ $deprecatedSites = merge $deprecatedSites (dict $url (dict
            "title" ($entry.title | default "")
            "category" ($entry.category | default "未知分类")
            "status" $status
            "description" ($entry.description | default "")
            "create_time" ($entry.create_time | default "")
            "update_time" ($entry.update_time | default "")
            "remark" ($entry.remark | default "")
            ))
        }}
    {{ end }}
{{ end }}

{{/* 第三步: 合并当前站点和废弃站点作为新站点 */}}
{{ $siteMap = merge $siteMap $deprecatedSites }}

{{ $debugContent := $siteMap | jsonify | truncate 100 }}
{{ partial "helpers/log" (dict "message" (printf "新数据内容（前100字符）: %s" $debugContent)) }}

{{/* 第四步: 计算有效站点个数、无效站点个数、新站点个数、分类信息等 */}}
{{ $validCount := 0 }}
{{ $invalidCount := 0 }}
{{ $newCount := 0 }}
{{ $categoryStats := dict }}
{{ range $site := $siteMap }}
    {{ if ne $site.status "deprecated" }}
        {{ $validCount = add $validCount 1 }}
    {{ end }}
    {{ if eq $site.status "deprecated" }}
        {{ $invalidCount = add $invalidCount 1 }}
    {{ end }}
    {{ if eq $site.status "new" }}
        {{ $newCount = add $newCount 1 }}
        {{ $category := $site.category }}
        {{ $currentCount := default 0 (index $categoryStats $category) }}
        {{ $categoryStats = merge $categoryStats (dict $category (add $currentCount 1)) }}
    {{ end }}
{{ end }}

{{/* 第五步: 构建数据 */}}
{{ $output := dict
    "meta" (dict
        "generated_at" (now.Format "2006-01-02 15:04:05")
        "stats" (dict
            "total" (len $siteMap)
            "valid" $validCount
            "deprecated" $invalidCount
            "new" (dict
                "count" $newCount
                "categories" $categoryStats
            )
        )
    )
    "sites" $siteMap
}}

{{/* 第六步: 返回数据 */}}
{{ return $output }}